// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import { Messages } from "../protobuf/Messages";
import { EventMgr } from "./EventManager";
import { Msg } from "./EventMsg";
import { socketEvent, socketManager } from "./Socket";
import { userManager } from "./UserManager";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Chat extends cc.Component {

    @property(cc.EditBox)
    editRoomId: cc.EditBox = null;
    @property(cc.Button)
    createRoomBtn: cc.Button = null;
    @property(cc.Button)
    joinRoomBtn: cc.Button = null;
    @property(cc.Label)
    chatContent: cc.Label = null;

    onLoad()
    {
        socketManager.initSocket(userManager.uid);
    }


    private registerEvent()
    {
        EventMgr.addEventListener(socketEvent.createRoom, this.HandleCreateRoom, this);
    }

    private HandleCreateRoom(eventName: string, eventData: any)
    {
        let response = Messages.CreateRoomResponse.decode(eventData);
        userManager.roomId = response.roomId;
    }

}
